package com.youhong.teethcare.settingpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.youhong.teethcare.BaseActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.customviews.ShSwitchView;
import com.youhong.teethcare.services.Common;

public class NotificationManagerActivity extends BaseActivity implements View.OnClickListener, ShSwitchView.OnSwitchStateChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_manager);
        initStatusBar(getResources().getColor(R.color.pageBackground),true);

        getViews();
    }

    ShSwitchView sv1, sv2, sv3, sv4, sv_sync;
    TextView tv_time1, tv_time2, tv_time3, tv_time4;
    TextView tv_dayOfWeek1, tv_dayOfWeek2, tv_dayOfWeek3, tv_dayOfWeek4;
    ImageView iv_back;

    RelativeLayout rl1, rl2, rl3, rl4;

    private void getViews() {

        rl1 = findViewById(R.id.notificationmanager_rl1);
        rl1.setOnClickListener(this);

        rl2 = findViewById(R.id.notificationmanager_rl2);
        rl2.setOnClickListener(this);

        rl3 = findViewById(R.id.notificationmanager_rl3);
        rl3.setOnClickListener(this);

        rl4 = findViewById(R.id.notificationmanager_rl4);
        rl4.setOnClickListener(this);

        sv1 = findViewById(R.id.notificationManager_sv1);
        sv2 = findViewById(R.id.notificationManager_sv2);
        sv3 = findViewById(R.id.notificationManager_sv3);
        sv4 = findViewById(R.id.notificationManager_sv4);
        sv_sync = findViewById(R.id.notificationManager_sv_sync);


        sv1.setOnSwitchStateChangeListener(this);
        sv2.setOnSwitchStateChangeListener(this);
        sv3.setOnSwitchStateChangeListener(this);
        sv4.setOnSwitchStateChangeListener(this);
        sv_sync.setOnSwitchStateChangeListener(this);

        iv_back = findViewById(R.id.personalInfo_iv_back);
        iv_back.setOnClickListener(this);
        tv_time1 = findViewById(R.id.notificationManager_tv_time1);
        tv_time2 = findViewById(R.id.notificationManager_tv_time2);
        tv_time3 = findViewById(R.id.notificationManager_tv_time3);
        tv_time4 = findViewById(R.id.notificationManager_tv_time4);

        tv_dayOfWeek1 = findViewById(R.id.notificationManager_tv_daysOfWeek1);
        tv_dayOfWeek2 = findViewById(R.id.notificationManager_tv_daysOfWeek2);
        tv_dayOfWeek3 = findViewById(R.id.notificationManager_tv_daysOfWeek3);
        tv_dayOfWeek4 = findViewById(R.id.notificationManager_tv_daysOfWeek4);



    }

    @Override
    public void onClick(View v) {
        if (v == iv_back) {

            this.finish();

        } else if (v == rl1) {
            Intent intent = new Intent(getApplicationContext(), NotificationSettingActivity.class);
            intent.putExtra("id", 1);
            startActivity(intent);

        } else if (v == rl2) {

            Intent intent = new Intent(getApplicationContext(), NotificationSettingActivity.class);
            intent.putExtra("id", 2);
            startActivity(intent);

        } else if (v == rl3) {

            Intent intent = new Intent(getApplicationContext(), NotificationSettingActivity.class);
            intent.putExtra("id", 3);
            startActivity(intent);

        } else if (v == rl4) {

            Intent intent = new Intent(getApplicationContext(), NotificationSettingActivity.class);
            intent.putExtra("id", 4);
            startActivity(intent);

        }
    }

    private String getDaysOfWeekString(int daysOfWeek) {

        StringBuilder builder = new StringBuilder();
        if (daysOfWeek == 0) {
            return "";
        }

        String[] weeksStr = getResources().getStringArray(R.array.weekarray2);

        if ((daysOfWeek & 0x1) != 0) {

            builder.append(weeksStr[0] + " ");

        }

        if ((daysOfWeek & 0x2) != 0) {

            builder.append(weeksStr[1] + " ");

        }
        if ((daysOfWeek & 0x4) != 0) {

            builder.append(weeksStr[2] + " ");

        }
        if ((daysOfWeek & 0x8) != 0) {

            builder.append(weeksStr[3] + " ");

        }

        if ((daysOfWeek & 0x10) != 0) {
            builder.append(weeksStr[4] + " ");
        }

        if ((daysOfWeek & 0x20) != 0) {
            builder.append(weeksStr[5] + " ");
        }

        if ((daysOfWeek & 0x40) != 0) {

            builder.append(weeksStr[6]);
        }
        return builder.toString();
    }

    @Override
    protected void onStart() {
        super.onStart();


        if (Common.userInfo.getDrinkingNotification()) {

            sv_sync.setOn(true);
        } else {

            sv_sync.setOn(false);
            sv1.setEnabled(false);
            sv2.setEnabled(false);
            sv3.setEnabled(false);
            sv4.setEnabled(false);
            rl1.setEnabled(false);
            rl2.setEnabled(false);
            rl3.setEnabled(false);
            rl4.setEnabled(false);

        }
        //Notification1
        {
            if (Common.userInfo.isClockSwitch1()) {
                sv1.setOn(true);
            }

            if (!TextUtils.isEmpty(Common.userInfo.getClockTime1())) {

                tv_time1.setText(Common.userInfo.getClockTime1());
            }


            tv_dayOfWeek1.setText(getDaysOfWeekString(Common.userInfo.getClockDayOfWeek1()));

        }

        //Notification2
        {
            if (Common.userInfo.isClockSwitch2()) {
                sv2.setOn(true);
            }

            if (!TextUtils.isEmpty(Common.userInfo.getClockTime2())) {

                tv_time2.setText(Common.userInfo.getClockTime2());
            }
            tv_dayOfWeek2.setText(getDaysOfWeekString(Common.userInfo.getClockDayOfWeek2()));

        }

        //Notification3
        {
            if (Common.userInfo.isClockSwitch3()) {
                sv3.setOn(true);
            }

            if (!TextUtils.isEmpty(Common.userInfo.getClockTime3())) {

                tv_time3.setText(Common.userInfo.getClockTime3());
            }
            tv_dayOfWeek3.setText(getDaysOfWeekString(Common.userInfo.getClockDayOfWeek3()));
        }

        //Notification4
        {
            if (Common.userInfo.isClockSwitch4()) {
                sv4.setOn(true);
            }
            if (!TextUtils.isEmpty(Common.userInfo.getClockTime4())) {

                tv_time4.setText(Common.userInfo.getClockTime4());
            }
            tv_dayOfWeek4.setText(getDaysOfWeekString(Common.userInfo.getClockDayOfWeek4()));

        }
    }

    @Override
    public void onSwitchStateChange(View view, boolean isOn) {

        if (view == sv1) {

            Common.userInfo.setClockSwitch1(isOn);

        } else if (view == sv2) {

            Common.userInfo.setClockSwitch2(isOn);

        } else if (view == sv3) {

            Common.userInfo.setClockSwitch3(isOn);

        } else if (view == sv4) {

            Common.userInfo.setClockSwitch4(isOn);

        } else if (sv_sync == view) {

            Common.userInfo.setDrinkingNotification(isOn);
            if (!isOn) {
                sv1.setOn(false, true);
                sv2.setOn(false, true);
                sv3.setOn(false, true);
                sv4.setOn(false, true);

                Common.userInfo.setClockSwitch1(false);
                Common.userInfo.setClockSwitch2(false);
                Common.userInfo.setClockSwitch3(false);
                Common.userInfo.setClockSwitch4(false);
                sv1.setEnabled(false);
                sv2.setEnabled(false);
                sv3.setEnabled(false);
                sv4.setEnabled(false);
                rl1.setEnabled(false);
                rl2.setEnabled(false);
                rl3.setEnabled(false);
                rl4.setEnabled(false);
            }else{

                sv1.setEnabled(true);
                sv2.setEnabled(true);
                sv3.setEnabled(true);
                sv4.setEnabled(true);
                rl1.setEnabled(true);
                rl2.setEnabled(true);
                rl3.setEnabled(true);
                rl4.setEnabled(true);



            }
        }

        Common.userInfo.SyncToLocal(getApplicationContext());
    }
}
